﻿using PostSystem.Properties;
using SoftwareBase.Handler;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;

namespace SoftwareBase.Verify
{
    public static class VerifyManager
    {
        private static Dictionary<long, string> Accounts = new Dictionary<long, string>
        {
            { 1, "token" },
            { 2, "token" },
            { 3, "token" },
            { 4, "token" },
            { 5, "token" },
        };

        private static Dictionary<long, byte> Chances = new Dictionary<long, byte>
        {
            { 1, 3 },
            { 2, 3 },
            { 3, 3 },
            { 4, 3 },
            { 5, 3 },
        };

        private static bool Login(long sno, string token) => Accounts.ContainsKey(sno) && Accounts[sno] == token;

        private static bool HaveChance(long sno) => Chances.ContainsKey(sno) && Chances[sno] > 0;

        private static void RemoveChance(long sno)
        {
            if (Chances.ContainsKey(sno))
            {
                Chances[sno]--;
            }
        }

        public static void LoadAccount()
        {
            var str = Resources.account;
            var lines = str.Split("\r\n");
            foreach (var line in lines)
            {
                var vs = line.Split("-");
                if (vs.Length < 2)
                {
                    break;
                }
                var sno = Convert.ToInt64(vs[0]);
                var password = vs[1];
                Accounts.Add(sno, password);
                Chances.Add(sno, 3);
            }
        }

        public static bool CheckUser(long sno, string password) =>  Login(sno, password);

        public static bool CheckFile(string video, string code) =>
            Path.GetExtension(video) == ".mp4" && Path.GetExtension(code) == ".zip";

        public static bool CheckChance (long sno)
        {
            if (HaveChance(sno))
            {
                RemoveChance(sno);
                return true;
            }
            return false;
        }


    }
}